package com.kandinfo.base.common.util.sftp;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import com.kandinfo.base.common.constant.WebConstants;

public class Sftp {

    private static final Logger logger = LoggerFactory.getLogger(Sftp.class);
    private static SFTPChannel channel = null;
    private static ChannelSftp chSftp = null;

    public static String getRealPath() {
        return "http://" + WebConstants.getProperty(WebConstants.SFTPHOST);
    }

    public static String getHost() {
        return WebConstants.getProperty(WebConstants.SFTPHOST);
    }

    public static boolean upload(InputStream is, String workDirectory, String dst) {
        try {
            chSftp.cd(workDirectory);
            OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE);
            byte[] buff = new byte[1024 * 256];

            int read;
            if (out != null) {
                logger.info("Start to read input stream");
                do {
                    read = is.read(buff, 0, buff.length);
                    if (read > 0) {
                        out.write(buff, 0, read);
                    }
                    out.flush();
                } while (read >= 0);
                logger.info("input stream read done.");
            }
            // 输出完成，关闭流
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            chSftp.quit();
            try {
                is.close();
                channel.closeChannel();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    /***
     * 
     * Description: 创建文件夹
     *
     * @param src
     * @return
     */
    public static boolean mkdir(String src) {
        if (chSftp != null) {
            try {
                if (!new File(src).exists())
                    chSftp.mkdir(src);
                return true;
            } catch (SftpException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 
     * Description: 删除文件
     *
     * @param src
     * @return
     */
    public static boolean rmdir(String src) {
        if (chSftp != null) {
            try {
                if (new File(src).exists())
                    chSftp.rm(src);
                return true;
            } catch (SftpException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 
     * Description: 获取链接
     *
     * @return
     */
    public static boolean getSFTPChannel() {
        try {
            channel = new SFTPChannel();
            chSftp = channel.getChannel();
            return true;
        } catch (JSchException e) {
            logger.error(e.getMessage(), e);
            logger.error("连接文件服务器异常！！！请求信息如下：host：" + WebConstants.getProperty(WebConstants.SFTPHOST) + ",username:"
                    + WebConstants.getProperty(WebConstants.SFTPUSERNAME) + ",password："
                    + WebConstants.getProperty(WebConstants.SFTPPASSWORD) + ",port:"
                    + WebConstants.getProperty(WebConstants.SFTPPORT));
            return false;
        }
    }

}
